package com.ruoyi.activity.entity;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.activity.domian.RedPacketRecord;
import com.ruoyi.common.utils.StringUtils;
import lombok.Data;

import java.util.Date;

@Data
public class RedPacketRecordQuery {
    /**
     * 红包类型
     */
    private Integer type;
    /**
     * 直播间id
     */
    private String roomId;
    /**
     * 用户uid
     */
    private String uid;
    /**
     * 红包是否抢光
     */
    private Boolean isFinish;
    /**
     * 红包最低金额查询
     */
    private Integer sendMinMoney;
    /**
     * 红包最高金额查询
     */
    private Integer sendMaxMoney;
    /**
     * 扣税金额
     */
    private Integer taxMinMoney;
    /**
     * 展示x轴数量
     */
    private Integer xAxisNum;
    /**
     * 选择
     */
    private Integer option;
    /**
     * 开始时间
     */
    private String beginTime;
    /**
     * 结束时间
     */
    private String endTime;
    /**
     * 第几页
     */
    private int pageNum;
    /**
     * 页码量
     */
    private int pageSize;

    public LambdaQueryWrapper<RedPacketRecord> getQuery(){
        LambdaQueryWrapper<RedPacketRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (type != null){
            queryWrapper.eq(RedPacketRecord::getType,type);
        }

        if (StringUtils.isNotEmpty(roomId)){
            queryWrapper.eq(RedPacketRecord::getRoomId,roomId);
        }

        if (StringUtils.isNotEmpty(uid)){
            queryWrapper.eq(RedPacketRecord::getUid, uid);
        }

        if (isFinish != null && isFinish){
            queryWrapper.eq(RedPacketRecord::getSurplusNum,0);
        }

        if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            queryWrapper.le(RedPacketRecord::getCreateTime,endTime);
            queryWrapper.ge(RedPacketRecord::getCreateTime,beginTime);
        }

        if (sendMaxMoney != null && sendMaxMoney > 0){
            queryWrapper.le(RedPacketRecord::getSendMoney,sendMaxMoney * 100);
        }

        if (sendMinMoney != null && sendMinMoney > 0){
            queryWrapper.ge(RedPacketRecord::getSendMoney,sendMinMoney * 100);
        }

        if (taxMinMoney != null && taxMinMoney > 0){
            queryWrapper.ge(RedPacketRecord::getTaxMoney, taxMinMoney * 100);
        }

        queryWrapper.orderByDesc(RedPacketRecord::getCreateTime);
        return queryWrapper;
    }

    public LambdaQueryWrapper<RedPacketRecord> getViewQuery(Date today){
        LambdaQueryWrapper<RedPacketRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (type != null){
            queryWrapper.eq(RedPacketRecord::getType,type);
        }

        if (StringUtils.isNotEmpty(roomId)){
            queryWrapper.eq(RedPacketRecord::getRoomId,roomId);
        }

        if (StringUtils.isNotEmpty(uid)){
            queryWrapper.eq(RedPacketRecord::getUid, uid);
        }

        if (isFinish != null && isFinish){
            queryWrapper.eq(RedPacketRecord::getSurplusNum,0);
        }

        if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            long day = DateUtil.between(DateUtil.parseDate(beginTime),DateUtil.parseDate(endTime), DateUnit.DAY);
            if (day >= 60L){
                return null;
            }
            queryWrapper.le(RedPacketRecord::getCreateTime,endTime);
            queryWrapper.ge(RedPacketRecord::getCreateTime,beginTime);
            this.xAxisNum = Math.toIntExact(day);
        }else{
            String day = DateUtil.format(DateUtil.offsetDay(today, 1 - xAxisNum), DatePattern.PURE_DATE_PATTERN);
            queryWrapper.ge(RedPacketRecord::getSendDate, day);
        }
        return queryWrapper;
    }
}
